package com.mxchip.livestar.ui.call


import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.mxchip.livestar.R
import com.mxchip.livestar.base.SystemData
import com.mxchip.livestar.base.hideSystemUI
import com.mxchip.livestar.base.toast
import com.mxchip.livestar.repository.RoomClientRepository
import com.mxchip.livestar.utils.MyLog
import com.videohigh.graham.SampleSDK
import kotlinx.android.synthetic.main.dialog_admin_mute.*


class AdminMuteDialogFragment : DialogFragment() {

    private val args: AdminMuteDialogFragmentArgs by navArgs()
    private var isMine = false

    // private val sampleSDK = SampleSDK.getInstance()

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view= inflater.inflate(R.layout.dialog_admin_mute, container, false)

        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val term = args.term
        val userName = args.userName

        if(term == null) {
            return
        }

        if(term.roomId == SystemData.roomId() && term.termId == SystemData.userId()) {
            isMine = true
        }

        text_title.text = term.termId

        val shortId = term.termId.trimStart('0')

        text_title.text = if(isMine) {
            "$shortId（本机）"
        } else {
            shortId
        }

        if (!userName.isNullOrBlank()) {
            text_title.text = userName
        }

        button_mic.setOnClickListener {

            if(isMine) {
                toast("请在本机完成静音操作")
                return@setOnClickListener
            }

            if(term == null) {
                return@setOnClickListener
            }

            RoomClientRepository.mgtMuteAudio(arrayOf(term))

            MyLog.logD(MyLog.ADMIN_MUTE_DIALOG_FRAGMENT_MODEL,"AdminMuteDialogFragment::onMicClick: message:${term}  ")

            findNavController().popBackStack()
        }

        button_camera.setOnClickListener {

            if(isMine) {
                toast("请在本机完成关闭摄像头操作")
                return@setOnClickListener
            }

            if(term == null) {
                return@setOnClickListener
            }

            RoomClientRepository.mgtMuteCamera(arrayOf(term))

            MyLog.logD(MyLog.ADMIN_MUTE_DIALOG_FRAGMENT_MODEL,"AdminMuteDialogFragment::onCameraClick: message:${term}  ")

            findNavController().popBackStack()
        }
    }

    override fun onStart() {
        context?.apply {
            val width = resources.displayMetrics.widthPixels * 0.4
            val height = ViewGroup.LayoutParams.WRAP_CONTENT //resources.displayMetrics.heightPixels * 0.8
            dialog?.window?.setLayout(width.toInt(), height)
        }

        dialog?.window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)

        super.onStart()

        dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    }

    override fun onResume() {
        super.onResume()
        dialog?.window?.hideSystemUI()
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        dialog.window?.decorView?.setOnSystemUiVisibilityChangeListener { visibility ->
            if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
                // adjustments to your UI, such as showing the action bar or
                // other navigational controls.
                dialog.window?.hideSystemUI()
            } else {
                // adjustments to your UI, such as hiding the action bar or
                // other navigational controls.
            }
        }
        return dialog
    }

    companion object {
        private const val TAG = "AdminMuteDialogFragment"
    }

}
